﻿using System.Xml;
using System.Xml.Linq;

namespace Ro.AssistFunction.TypeConversion
{
    /// <summary>
    /// XmlNode和Xelement的互相转换
    /// </summary>
    public class XmlAndXElement
    {
        /// <summary>
        /// 将XmlNode单独包装为一个XElement传出
        /// </summary>
        /// <param name="sigNode">XmlNode格式节点</param>
        /// <returns>XElement格式节点</returns>
        public XElement ToXElement(XmlNode sigNode)
        {
            //更新方法
            XDocument xDoc = new XDocument();
            using (XmlWriter xmlWriter = xDoc.CreateWriter())
            {
                sigNode.WriteTo(xmlWriter);
            }
            return xDoc.Root;
        }


        /// <summary>
        /// 将XElement单独包装为一个XmlNode传出
        /// </summary>
        /// <param name="element">XElement格式节点</param>
        /// <returns>XmlNode格式节点</returns>
        public XmlNode ToXmlNode(XElement element)
        {
            using (XmlReader xmlReader = element.CreateReader())
            {
                XmlDocument xmlDoc = new XmlDocument();
                return xmlDoc.ReadNode(xmlReader);
            }
        }
    }
}
